<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 怪物大师
  Date: 2024/4/26
  Time: 21:56
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //获取是否初始化 当start不存在时初始化
    String start = request.getParameter("start");
    if (start!=null){
        int i=0;
        session.setAttribute("i",i);
        //生成随机字母
//        char zimu=(char)(Math.random()*26+97);
        char zimu='a';
        session.setAttribute("zimu",zimu);
        session.setAttribute("start",true);
        session.setAttribute("message","开始你的猜测");
        } else {
        String caice = request.getParameter("caice");
        char zimu='1';
        if (session.getAttribute("zimu")!=null)
             zimu = (char) session.getAttribute("zimu");
        int i = 0;
        if (session.getAttribute("i")!=null)
            i= (int) session.getAttribute("i");
        i++;
        if (caice==null){

        }else if(caice.length()>1)//如果猜测的长度大于1或者不对
        {
            session.setAttribute("i",i);
            String message="你猜错了,这是第"+i+"次猜测";
            session.setAttribute("message",message);
        }else if(!(caice.charAt(0)==zimu)){
            session.setAttribute("i",i);
            String message="你猜错了,这是第"+i+"次猜测";
            session.setAttribute("message",message);
        }else {
            System.out.println("aaaaaa");
            session.setAttribute("i",i);
            session.removeAttribute("start");
            session.removeAttribute("zimu");
        }

    }


%>
<c:choose>
    <c:when test="${start}">
        当前猜测结果:${message}<br>
        输入你的猜测
        <form action="guess.jsp">
            <input type="text" name="caice">
            <input type="submit" value="提交">
        </form>
    </c:when>
    <c:otherwise>
        当前猜测结果:你猜对了，这是第${i}次猜测<br>
        <a href="guess.jsp?start=true">重新获取随机数</a>
    </c:otherwise>
</c:choose>

</body>
</html>
